package com.ruiyian.bridge.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruiyian.bridge.info.domain.Mmh2oInfo;
import com.ruiyian.bridge.info.domain.SensorWarningLog;
import com.ruiyian.bridge.info.mapper.SensorWarningLogMapper;
import com.ruiyian.bridge.info.service.Mmh2oInfoService;
import com.ruiyian.bridge.info.service.SensorWarningLogService;
import com.ruiyian.bridge.project.domain.DeviceWarnSet;
import com.ruiyian.bridge.project.domain.MeasurePoint;
import com.ruiyian.bridge.project.enums.DeviceTypeEnum;
import com.ruiyian.bridge.project.service.DeviceWarnSetService;
import com.ruiyian.bridge.project.service.MeasurePointfoService;
import com.ruiyian.common.utils.DateUtils;
import com.ruiyian.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/6/17
 * Desc: 超限预警日志
 */
@Service
public class SensorWarningLogServiceImpl extends ServiceImpl<SensorWarningLogMapper,SensorWarningLog> implements  SensorWarningLogService {
    @Autowired
    private SensorWarningLogMapper sensorWarningLogMapper;
    @Autowired
    private DeviceWarnSetService deviceWarnSetService;
    @Autowired
    private MeasurePointfoService measurePointfoService;
    @Autowired
    private Mmh2oInfoService mmh2oInfoService;

    @Override
    public List<SensorWarningLog> list(SensorWarningLog sensorWarningLog) {
        return sensorWarningLogMapper.list(sensorWarningLog);
    }
    /**
     * 处理
     * @param sensorWarningLog
     */
    @Override
    public void deal(SensorWarningLog sensorWarningLog) {
        sensorWarningLog.setSettleFlag(true);
        sensorWarningLog.setSettleUser(SecurityUtils.getUsername());
        sensorWarningLogMapper.deal(sensorWarningLog);
    }
    /**
     * 添加超限预警数据
     */
    @Override
    public void addWarnLog() {
        // 获取超限预警值
        DeviceWarnSet deviceWarnSet = null;
        List<DeviceWarnSet> deviceWarnSets = deviceWarnSetService.selectTSensorWarnSetList(new DeviceWarnSet());
        if(deviceWarnSets.size()>0){
            deviceWarnSet = deviceWarnSets.get(0);
        }else {
            return;
        }
        //待添加的预警数据
        List<SensorWarningLog> logList = new ArrayList<SensorWarningLog>();
//        List<SysSms> smsList = new ArrayList<SysSms>();
        // 测点数据
//        List<MeasurePoint> measurePoints = measurePointfoService.selectTSensorTypeList(new MeasurePoint());

        //查询沉降超限数据 时间范围查询
        String date = DateUtils.getNextDate(2, new Date(), -2, DateUtils.datetimeFormat.get());
        //查询近一小时的沉降数据
        List<Mmh2oInfo> mmhList = mmh2oInfoService.queryDataAbove(deviceWarnSet.getDtcjLimitUpVlue(),date);
        if(mmhList.size()>0){
           for(Mmh2oInfo m : mmhList){
               QueryWrapper<MeasurePoint> wrapper = new QueryWrapper<>();
               wrapper.eq("addr",m.getAddr());
               wrapper.eq("macaddr",m.getMacaddr());
               MeasurePoint measurePoint = measurePointfoService.getOne(wrapper);
               DeviceWarnSet ds = checkWarnLevel(deviceWarnSets, DeviceTypeEnum.MMH2O.getType(), Double.parseDouble(m.getJgvalue()));
               if(ds == null){
                   return;
               }
               if(null== measurePoint){
                   return;
               }
               SensorWarningLog log = new SensorWarningLog();
               log.setBridgeId(measurePoint.getBridgeId());
               log.setSensorId(measurePoint.getId());
               log.setSensorNo(measurePoint.getSensorNo());
               log.setSensorType(measurePoint.getSensorType());
               log.setSensorTime(m.getCreattime());
               log.setSensorVlue(Double.parseDouble(m.getJgvalue()));
               log.setSettleFlag(false);
               log.setCreateTime(new Date());
               log.setWarnDgree(ds.getWarnDgree());
               log.setSensorAddress(measurePoint.getSensorAddress());
               log.setSensorWarnId(ds.getId().toString());
               log.setLimitUpVlue(ds.getDtcjLimitUpVlue());
               logList.add(log);

           }
        }
        this.saveBatch(logList);
    }

    DeviceWarnSet checkWarnLevel(List<DeviceWarnSet> deviceWarnSets,Integer type,Double jgvalue){
        DeviceWarnSet dw = null;
        for(DeviceWarnSet f: deviceWarnSets){
            if(type == DeviceTypeEnum.MMH2O.getType()){
                if(jgvalue>=f.getDtcjLimitUpVlue()){
                    dw = f;
                }else {
                    break;
                }
            }
        }
        return dw;
    }

    @Override
    public List<Map<String, Object>> countWarnDgree() {
        return sensorWarningLogMapper.countWarnDgree();
    }

    @Override
    public List<Map<String, Object>> countSensorType() {
        return sensorWarningLogMapper.countSensorType();
    }
}
